﻿Imports System.Data.OleDb

Public Class NewItem
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Only teachers can see answers to questions
        If Roles.IsUserInRole("teacher") Then
            Panel1.Visible = True
            Label1.Visible = False
        Else
            Panel1.Visible = False
            Label1.Visible = True
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_new_question.Click

        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"

        If f_attachment.HasFile Then
            f_attachment.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into question(questionText, pointValue, answer, attachment, examBoardID, examTypeID, SubjectID, author) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_questionText.Text)
        cmd.Parameters.AddWithValue("@f2", tb_pointValue.Text)
        cmd.Parameters.AddWithValue("@f3", tb_answer.Text)
        cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", tb_examBoard.Text)
        cmd.Parameters.AddWithValue("@f6", tb_examType.Text)
        cmd.Parameters.AddWithValue("@f7", tb_subject.Text)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("Confirmation.aspx")


    End Sub


End Class